home *** CD-ROM | disk | FTP | other *** search
/ The Utilities Experience / The Utilities Experience - Volume 1.iso / software / misc / o-z / x-windows / gs262 / gdevtiff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-29  |  26.9 KB  |  781 lines

  1. /* $Header: /u/staff/tim/gs262/RCS/gdevtiff.c,v 1.3 1993/03/20 11:44:02 sam Exp $ */
  2.  
  3. /*
  4.  * Copyright (c) 1992, 1993 Sam Leffler
  5.  * Copyright (c) 1992, 1993 Silicon Graphics, Inc.
  6.  *
  7.  * Permission to use, copy, modify, distribute, and sell this software and 
  8.  * its documentation for any purpose is hereby granted without fee, provided
  9.  * that (i) the above copyright notices and this permission notice appear in
  10.  * all copies of the software and related documentation, and (ii) the names of
  11.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  12.  * publicity relating to the software without the specific, prior written
  13.  * permission of Sam Leffler and Silicon Graphics.
  14.  * 
  15.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  16.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  17.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  18.  * 
  19.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  20.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  21.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  22.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  23.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  24.  * OF THIS SOFTWARE.
  25.  */
  26. /*
  27.  * 5/19/93 modified by L. Peter Deutsch, Aladdin Enterprises,
  28.  *   for compatibility with Ghostscript 2.6.1.
  29.  */
  30. /* gdevtiff.c */
  31.  
  32. #include "gdevprn.h"
  33. #include "gdevdfg3.h"
  34. #include "gdevtiff.h"
  35.  
  36. #ifdef __PROTOTYPES__
  37. #    define PROTO_ARGS(X)    X
  38. #else
  39. #    define PROTO_ARGS(X)    ()
  40. #endif
  41.  
  42. /*
  43.  * TIFF fax output driver.
  44.  */
  45. typedef struct {
  46.     FILE*    fp;
  47.     long    prevdir;    /* file offset of previous directory offset */
  48.     long    diroff;        /* file offset of next write */
  49.     int        bigendian;    /* 1 if machine is big-endian, 0 otherwise */
  50.     unsigned long iwidth;    /* width of image data in pixels */
  51.     int        fax_byte;
  52.     int        fax_weight;
  53. } TIFFOUT;
  54.  
  55. private void    faxout_open_fp PROTO_ARGS((FILE *, TIFFOUT*));
  56. private int    faxout_begin_page PROTO_ARGS((TIFFOUT*, gx_device_printer*));
  57. #ifdef NON_USATA
  58. private int    faxout_eolcode PROTO_ARGS((TIFFOUT *));
  59. #endif
  60. private int    faxout_end_page PROTO_ARGS((TIFFOUT *));
  61. private void    tofax PROTO_ARGS((TIFFOUT*, unsigned char*));
  62.  
  63. #ifndef AMIGA
  64. private void putwhitespan();
  65. private void putblackspan();
  66. private void putcode();
  67. private void puteol();
  68. private void putbit();
  69. private void flushbits();
  70. #else
  71. private void putwhitespan(register TIFFOUT *, int);
  72. private void putblackspan(register TIFFOUT *, int);
  73. private void putcode(register TIFFOUT *, tableentry *);
  74. private void puteol(register TIFFOUT *);
  75. private void putbit(register TIFFOUT *, int);
  76. private void flushbits(register TIFFOUT *);
  77. #endif
  78.  
  79. /*
  80.  * Redefine the device descriptor.
  81.  */
  82. struct gx_device_tiff_s {
  83.     gx_device_common;
  84.     gx_prn_device_common;
  85.     TIFFOUT    fax;
  86. };
  87. typedef struct gx_device_tiff_s gx_device_tiff;
  88.  
  89. /* The device descriptor */
  90. #define X_DPI 204
  91. #define Y_DPI 196
  92. #define LINE_SIZE ((X_DPI * 101 / 10 + 7) / 8)    /* bytes per line */
  93.  
  94. private dev_proc_open_device(tiff_prn_open);
  95. private dev_proc_print_page(tiff_print_page);
  96. private dev_proc_close_device(tiff_prn_close);
  97.  
  98. gx_device_procs tiff_std_procs =
  99.     prn_procs(tiff_prn_open, gdev_prn_output_page, tiff_prn_close);
  100.  
  101. gx_device_tiff far_data gs_tiffg3_device =
  102. {   prn_device_std_body(
  103.     gx_device_tiff,
  104.     tiff_std_procs,
  105.     "tiffg3",
  106.     85,            /* width_10ths, 8.5" */
  107.     110,            /* height_10ths, 11" */
  108.     X_DPI, Y_DPI,
  109.     0,0,0,0,        /* margins */
  110.     1,
  111.     tiff_print_page
  112.     )
  113. };
  114.  
  115.  
  116. static struct pageinfo {
  117.     short w, h;            /* page width and height in 10ths */
  118.     unsigned long iw;        /* image width */
  119. } pageinfo[] = {
  120. #define    PAPER_SIZE_LETTER    0
  121.     { 85,  110, 1728 },
  122. #define    PAPER_SIZE_A4        1
  123.     { 85,  117, 1728 },
  124. #define    PAPER_SIZE_B4        2
  125.     { 101, 143, 2048 }
  126. };
  127. #define    NPAGEINFO (sizeof (pageinfo) / sizeof (pageinfo[0]))
  128.  
  129. /* Get the paper size code, based on width and height. */
  130. static int
  131. papersize(gx_device *dev)
  132. {
  133.     return
  134.       (dev->height / dev->y_pixels_per_inch >= 11.8 ? PAPER_SIZE_B4 :
  135.        dev->height / dev->y_pixels_per_inch >= 11.1 ? PAPER_SIZE_A4 :
  136.        PAPER_SIZE_LETTER);
  137. }
  138.  
  139. /*
  140.  * Driver entry points.
  141.  */
  142.  
  143. /*
  144.  * Setup device according to output page.
  145.  */
  146. private int
  147. tiff_prn_open(gx_device *pdev)
  148. {
  149.     struct pageinfo* pi = &pageinfo[papersize(pdev)];
  150.     int    rc;
  151.  
  152.     pdev->width = (int)((pi->w * pdev->x_pixels_per_inch) / 10);
  153.     pdev->height = (int)((pi->h * pdev->y_pixels_per_inch) / 10);
  154.     rc = gdev_prn_open(pdev);
  155.     if (rc == 0) {
  156.     gx_device_tiff* ddev = (gx_device_tiff*) pdev;
  157.     faxout_open_fp(ddev->file, &ddev->fax);
  158.     ddev->fax.iwidth = pi->iw;
  159.     }
  160.     return (rc);
  161. }
  162.  
  163. private int
  164. tiff_print_page(gx_device_printer *pdev, FILE *prn_stream)
  165. {
  166.     gx_device_tiff* ddev = (gx_device_tiff*) pdev;
  167.     unsigned char data[LINE_SIZE + 4];
  168.     int    lnum, line_size;
  169.     TIFFOUT* fax = &ddev->fax;
  170.  
  171.     /* For some odd reason, the file isn't open until now */
  172.     fax->fp = prn_stream;    
  173.     faxout_begin_page(fax, pdev);
  174.     line_size = gdev_mem_bytes_per_scan_line((gx_device*)pdev);
  175.     for (lnum = 0; lnum < pdev->height; lnum++) {
  176.     gdev_prn_copy_scan_lines(pdev, lnum, (byte *)data, line_size);
  177.     tofax(fax, data);
  178.     }
  179.     faxout_end_page(fax);
  180.     return (0);
  181. }
  182.  
  183. private int
  184. tiff_prn_close(gx_device *pdev)
  185. {
  186.     gx_device_tiff* ddev = (gx_device_tiff*) pdev;
  187.     TIFFOUT* fax = &ddev->fax;
  188.  
  189.     if (fax->fp)
  190.     fflush(fax->fp);
  191.     return (gdev_prn_close(pdev));
  192. }
  193.  
  194. /*
  195.  * Internal routines.
  196.  */
  197. private void
  198. faxout_open_fp(FILE *fp, register TIFFOUT* faxp)
  199. {
  200.     faxp->fp = fp;
  201.     faxp->diroff = 0L;
  202.     faxp->prevdir = 0L;
  203.     faxp->bigendian = arch_is_big_endian;
  204.     faxp->fax_byte = 0;
  205.     faxp->fax_weight = 0x80;
  206. }
  207.  
  208. /* NB: this array is sorted by tag number (assumed below) */
  209. typedef struct {
  210.     TIFFDirEntry    subfiletype;
  211.     TIFFDirEntry    imagewidth;
  212.     TIFFDirEntry    imagelength;
  213.     TIFFDirEntry    bitspersample;
  214.     TIFFDirEntry    compression;
  215.     TIFFDirEntry    photometric;
  216.     TIFFDirEntry    fillorder;
  217. #ifdef notdef
  218.     TIFFDirEntry    documentname;
  219. #endif
  220.     TIFFDirEntry    stripoffsets;
  221.     TIFFDirEntry    orientation;
  222.     TIFFDirEntry    samplesperpixel;
  223.     TIFFDirEntry    rowsperstrip;
  224.     TIFFDirEntry    stripbytecounts;
  225.     TIFFDirEntry    xresolution;
  226.     TIFFDirEntry    yresolution;
  227.     TIFFDirEntry    planarconfig;
  228.     TIFFDirEntry    group3options;
  229.     TIFFDirEntry    resolutionunit;
  230. #ifdef notdef
  231.     TIFFDirEntry    software;
  232. #endif
  233.     TIFFDirEntry    cleanfaxdata;
  234.     unsigned long    diroff;            /* offset to next directory */
  235.     unsigned long    xresValue[2];        /* xresolution indirect value */
  236.     unsigned long    yresValue[2];        /* yresolution indirect value */
  237. } TIFFDirectory;
  238. private TIFFDirectory dirTemplate = {
  239.     { TIFFTAG_SUBFILETYPE,    TIFF_LONG,  1, FILETYPE_PAGE },
  240.     { TIFFTAG_IMAGEWIDTH,    TIFF_LONG,  1 },
  241.     { TIFFTAG_IMAGELENGTH,    TIFF_LONG,  1 },
  242.     { TIFFTAG_BITSPERSAMPLE,    TIFF_SHORT, 1, 1 },
  243.     { TIFFTAG_COMPRESSION,    TIFF_SHORT, 1, COMPRESSION_CCITTFAX3 },
  244.     { TIFFTAG_PHOTOMETRIC,    TIFF_SHORT, 1, PHOTOMETRIC_MINISWHITE },
  245.     { TIFFTAG_FILLORDER,    TIFF_SHORT, 1, FILLORDER_MSB2LSB },
  246. #ifdef notdef
  247.     { TIFFTAG_DOCUMENTNAME,    TIFF_ASCII, 1 },
  248. #endif
  249.     { TIFFTAG_STRIPOFFSETS,    TIFF_LONG,  1 },
  250.     { TIFFTAG_ORIENTATION,    TIFF_SHORT, 1, ORIENTATION_TOPLEFT },
  251.     { TIFFTAG_SAMPLESPERPIXEL,    TIFF_SHORT, 1, 1 },
  252.     { TIFFTAG_ROWSPERSTRIP,    TIFF_LONG,  1, -1L },
  253.     { TIFFTAG_STRIPBYTECOUNTS,    TIFF_LONG,  1, 1 },
  254.     { TIFFTAG_XRESOLUTION,    TIFF_RATIONAL, 1 },
  255.     { TIFFTAG_YRESOLUTION,    TIFF_RATIONAL, 1 },
  256.     { TIFFTAG_PLANARCONFIG,    TIFF_SHORT, 1, PLANARCONFIG_CONTIG },
  257.     { TIFFTAG_GROUP3OPTIONS,    TIFF_LONG,  1 },
  258.     { TIFFTAG_RESOLUTIONUNIT,    TIFF_SHORT, 1, RESUNIT_INCH },
  259. #ifdef notdef
  260.     { TIFFTAG_SOFTWARE,        TIFF_ASCII, 1 },
  261. #endif
  262.     { TIFFTAG_CLEANFAXDATA,    TIFF_SHORT, 1, CLEANFAXDATA_CLEAN },
  263.     0, { 0, 1 }, { 0, 1 },
  264. };
  265. #define    OFFSET(x)    ((unsigned)&(((TIFFDirectory*)0)->x))
  266. #define    NTAGS        (OFFSET(diroff) / sizeof (TIFFDirEntry))
  267.  
  268. /* correct tag values on bigendian machines */
  269. private void
  270. faxout_fixuptags(TIFFDirEntry* dp, int n)
  271. {
  272.     while (n-- > 0) {
  273.     if (dp->tdir_type == TIFF_SHORT || dp->tdir_type == TIFF_SSHORT)
  274.         dp->tdir_offset <<= 16;
  275.     else if (dp->tdir_type == TIFF_BYTE || dp->tdir_type == TIFF_SBYTE)
  276.         dp->tdir_offset <<= 24;
  277.     dp++;
  278.     }
  279. }
  280.  
  281. private int
  282. faxout_begin_page(TIFFOUT *faxp, gx_device_printer* pdev)
  283. {
  284.     gx_device_tiff* ddev = (gx_device_tiff*) pdev;
  285.     short dircount;
  286.     TIFFDirectory dir;
  287.  
  288.     /*
  289.      * Writing the header is delayed to here because the
  290.      * FILE* is not setup when faxout_open is called.
  291.      */
  292.     if (faxp->diroff == 0) {
  293.     TIFFHeader h;
  294.     h.tiff_magic = (faxp->bigendian ? TIFF_BIGENDIAN : TIFF_LITTLEENDIAN);
  295.     h.tiff_version = TIFF_VERSION;
  296.     h.tiff_diroff = sizeof (TIFFHeader);
  297.     fwrite((char*) &h, sizeof (h), 1, faxp->fp);
  298.     faxp->diroff = sizeof (TIFFHeader);    /* where next directory goes */
  299.     } else {
  300.     /* patch pointer to this directory */
  301.     fseek(faxp->fp, faxp->prevdir, 0);
  302.     fwrite((char*)&faxp->diroff, sizeof (faxp->diroff), 1, faxp->fp);
  303.     }
  304.     fseek(faxp->fp, faxp->diroff, 0);
  305.     /* write count of tags in directory */
  306.     dircount = NTAGS;
  307.     fwrite((char*)&dircount, sizeof (dircount), 1, faxp->fp);
  308.     faxp->diroff += sizeof (dircount);
  309.  
  310.     /* fill in directory tags and write them */
  311.     memcpy(&dir, &dirTemplate, sizeof (dirTemplate));
  312.     dir.imagewidth.tdir_offset = pageinfo[papersize((gx_device*) pdev)].iw;
  313.     dir.imagelength.tdir_offset = pdev->height;
  314.     dir.stripoffsets.tdir_offset = faxp->diroff + sizeof (TIFFDirectory);
  315.     dir.xresolution.tdir_offset = faxp->diroff + OFFSET(xresValue);
  316.     dir.yresolution.tdir_offset = faxp->diroff + OFFSET(yresValue);
  317.     dir.group3options.tdir_offset = 0;        /* XXX */
  318.     dir.xresValue[0] = ddev->x_pixels_per_inch;
  319.     dir.yresValue[0] = ddev->y_pixels_per_inch;
  320.     if (faxp->bigendian)
  321.     faxout_fixuptags(&dir.subfiletype, NTAGS);
  322.     fwrite((char*)&dir, sizeof (dir), 1, faxp->fp);
  323.  
  324.     puteol(faxp);
  325.     return (0);
  326. }
  327.  
  328. private int
  329. faxout_end_page(TIFFOUT *faxp)
  330. {
  331.     long diroff, cc;
  332.  
  333.     flushbits(faxp);
  334.     diroff = faxp->diroff;
  335.     faxp->prevdir = faxp->diroff + OFFSET(diroff);
  336.     faxp->diroff = ftell(faxp->fp);
  337.     /* patch strip byte counts value */
  338.     cc = faxp->diroff - (diroff + sizeof (TIFFDirectory));
  339.     fseek(faxp->fp, diroff + OFFSET(stripbytecounts.tdir_offset), 0);
  340.     fwrite(&cc, sizeof (cc), 1, faxp->fp);
  341.     return (0);
  342. }
  343.  
  344. private const byte far_data b_run_tbl[8][256] = {
  345.   {    /* START BIT 0 */
  346.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  347.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  348.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  349.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  350.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  351.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  352.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  353.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  354.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  355.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  356.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  357.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  358.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  359.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  360.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  361.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  362.   },
  363.   {    /* START BIT 1 */
  364.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  365.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  366.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  367.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  368.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  369.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  370.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  371.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  372.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  373.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  374.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  375.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  376.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  377.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  378.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  379.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  380.   },
  381.   {    /* START BIT 2 */
  382.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  383.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  384.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  385.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  386.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  387.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  388.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  389.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  390.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  391.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  392.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  393.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  394.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  395.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  396.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  397.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  398.   },
  399.   {    /* START BIT 3 */
  400.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  401.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  402.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  403.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  404.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  405.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  406.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  407.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  408.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  409.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  410.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  411.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  412.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  413.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  414.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  415.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  416.   },
  417.   {    /* START BIT 4 */
  418.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  419.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  420.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  421.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  422.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  423.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  424.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  425.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  426.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  427.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  428.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  429.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  430.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  431.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  432.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  433.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  434.   },
  435.   {    /* START BIT 5 */
  436.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  437.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  438.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  439.     2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6, 
  440.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  441.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  442.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  443.     2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6, 
  444.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  445.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  446.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  447.     2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6, 
  448.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  449.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  450.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  451.     2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6, 
  452.   },
  453.   {    /* START BIT 6 */
  454.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  455.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  456.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  457.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  458.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  459.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  460.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  461.     3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 6, 7, 
  462.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  463.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  464.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  465.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  466.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  467.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  468.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  469.     3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 6, 7, 
  470.   },
  471.   {    /* START BIT 7 */
  472.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  473.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  474.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  475.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  476.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  477.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  478.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  479.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  480.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  481.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  482.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  483.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  484.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  485.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  486.     3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
  487.     4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 8, 
  488.   },
  489. };
  490.  
  491. private const byte far_data w_run_tbl[8][256] = {
  492.   {    /* START BIT 0 */
  493.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  494.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  495.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  496.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  497.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  498.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  499.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  500.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  501.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  502.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  503.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  504.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  505.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  506.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  507.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  508.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  509.   },
  510.   {    /* START BIT 1 */
  511.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  512.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  513.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  514.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  515.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  516.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  517.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  518.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  519.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  520.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  521.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  522.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  523.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  524.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  525.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  526.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  527.   },
  528.   {    /* START BIT 2 */
  529.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  530.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  531.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  532.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  533.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  534.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  535.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  536.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  537.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  538.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  539.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  540.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  541.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  542.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  543.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  544.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  545.   },
  546.   {    /* START BIT 3 */
  547.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  548.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  549.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  550.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  551.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  552.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  553.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  554.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  555.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  556.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  557.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  558.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  559.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  560.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  561.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  562.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  563.   },
  564.   {    /* START BIT 4 */
  565.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  566.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  567.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  568.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  569.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  570.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  571.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  572.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  573.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  574.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  575.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  576.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  577.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  578.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  579.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  580.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  581.   },
  582.   {    /* START BIT 5 */
  583.     6, 5, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 
  584.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  585.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  586.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  587.     6, 5, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 
  588.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  589.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  590.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  591.     6, 5, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 
  592.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  593.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  594.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  595.     6, 5, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 
  596.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  597.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  598.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  599.   },
  600.   {    /* START BIT 6 */
  601.     7, 6, 5, 5, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 
  602.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  603.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  604.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  605.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  606.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  607.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  608.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  609.     7, 6, 5, 5, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 
  610.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  611.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  612.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  613.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  614.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  615.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  616.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  617.   },
  618.   {    /* START BIT 7 */
  619.     8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 
  620.     3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
  621.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  622.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  623.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  624.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  625.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  626.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  627.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  628.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  629.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  630.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  631.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  632.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  633.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  634.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  635.   },
  636. };
  637.  
  638. /*
  639.  * Macros to use tables in findrun.c
  640.  * Input is *p, bit
  641.  * Output is rl, *p, bit
  642.  */
  643. #define    find_black_run()                    \
  644.     for (rl = 0;;) {                        \
  645.     if (run = b_run_tbl[bit][*p]) {                \
  646.         rl += run;                        \
  647.         bit -= run;                        \
  648.         if (bit < 0 && ++p < ep) { bit=7; continue;}    \
  649.     }                            \
  650.     break;                            \
  651.     }
  652. #define    find_white_run()                    \
  653.     for (rl = 0;;) {                        \
  654.     if (run = w_run_tbl[bit][*p]) {                \
  655.         rl += run;                        \
  656.         bit -= run;                        \
  657.         if (bit < 0 && ++p < ep) { bit=7; continue;}    \
  658.     }                            \
  659.     break;                            \
  660.     }
  661.  
  662. private void
  663. tofax(TIFFOUT *faxp, unsigned char *p)
  664. {
  665.     unsigned char    *ep;
  666.     register int    bit;
  667.     register int    run;
  668.     register int    rl;
  669.  
  670.     ep = p + faxp->iwidth/8;
  671.     bit = 7;
  672.     for (;;) {
  673.     find_white_run();
  674.     putwhitespan(faxp, rl);
  675.     if (p >= ep) break;
  676.  
  677.     find_black_run();
  678.     putblackspan(faxp, rl);
  679.     if (p >= ep) break;
  680.     }
  681.     puteol(faxp);
  682. }
  683.  
  684.  
  685. /*************************************************************************
  686. **
  687. ** Copyright (C) 1989 by Paul Haeberli <paul@manray.sgi.com>.
  688. **
  689. ** Permission to use, copy, modify, and distribute this software and its
  690. ** documentation for any purpose and without fee is hereby granted, provided
  691. ** that the above copyright notice appear in all copies and that both that
  692. ** copyright notice and this permission notice appear in supporting
  693. ** documentation.  This software is provided "as is" without express or
  694. ** implied warranty.
  695.  *************************************************************************/
  696.  
  697. private void
  698. putwhitespan(register TIFFOUT *faxp, int c)
  699. {
  700.     register int tpos;
  701.     register tableentry* te;
  702.  
  703.     if(c>=64) {
  704.     tpos = (c/64)-1;
  705.     te = tpos < 27 ? mwtable+tpos : extable+tpos-27;
  706.     c -= te->count;
  707.     putcode(faxp, te);
  708.     }
  709.     tpos = c;
  710.     te = twtable+tpos;
  711.     putcode(faxp, te);
  712. }
  713.  
  714. private void
  715. putblackspan(register TIFFOUT *faxp, int c)
  716. {
  717.     register int tpos;
  718.     register tableentry* te;
  719.  
  720.     if(c>=64) {
  721.     tpos = (c/64)-1;
  722.     te = tpos < 27 ? mbtable+tpos : extable+tpos-27;
  723.     c -= te->count;
  724.     putcode(faxp, te);
  725.     }
  726.     tpos = c;
  727.     te = tbtable+tpos;
  728.     putcode(faxp, te);
  729. }
  730.  
  731. private void
  732. putcode(register TIFFOUT *faxp, tableentry *te)
  733. {
  734.     register unsigned int mask;
  735.     register int code;
  736.  
  737.     mask = 1<<(te->length-1);
  738.     code = te->code;
  739.     while(mask) {
  740.      if(code&mask)
  741.         putbit(faxp, 1);
  742.     else
  743.         putbit(faxp, 0);
  744.     mask >>= 1;
  745.     }
  746.  
  747. }
  748.  
  749. private void
  750. puteol(register TIFFOUT *faxp)
  751. {
  752.     register int i;
  753.  
  754.     for(i=0; i<11; ++i)
  755.     putbit(faxp, 0);
  756.     putbit(faxp, 1);
  757. }
  758.  
  759. private void
  760. putbit(register TIFFOUT *faxp, int d)
  761. {
  762.     if(d) 
  763.     faxp->fax_byte = faxp->fax_byte|faxp->fax_weight;
  764.     faxp->fax_weight = faxp->fax_weight>>1;
  765.     if((faxp->fax_weight&0xff) == 0) {
  766.     putc(faxp->fax_byte, faxp->fp);
  767.     faxp->fax_byte = 0;
  768.     faxp->fax_weight = 0x80;
  769.     }
  770. }
  771.  
  772. private void
  773. flushbits(register TIFFOUT *faxp)
  774. {
  775.     if (faxp->fax_weight != 0x80) {
  776.     putc(faxp->fax_byte, faxp->fp);
  777.     faxp->fax_byte = 0;
  778.     faxp->fax_weight = 0x80;
  779.     }
  780. }
  781.